#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "like.h"
#include "socket.h"
#include "epoll.h"

userinfo *like_init()
{
	userinfo *head;
	head = (userinfo *)malloc(sizeof(userinfo));
	if(head ==NULL)
		perr_exit("malloc error");
	head->next=NULL;
	return head;
}

int Insert_like(user_data user,userinfo *head)
{
	userinfo *end;
	end = (userinfo *)malloc(sizeof(userinfo));
	if(end == NULL)
		return -1;

	end->user.fd = user.fd;
	end->user.mark = user.mark;
	strcpy(end->user.name,user.name);

	end->next = head->next;
	head->next = end;

	return 0;
}
int ergodic_list(const char*name,userinfo *head)
{
	userinfo *p;
	p=head->next;
	while(p!=NULL){
		if((strcmp(p->user.name,name))==0)
			return -1;
		p=p->next;
	}
	return 0;
}

int fd_ergodic_list(const char *name ,userinfo *head)
{
	userinfo *p;
	p=head->next;
	while(p!=NULL){
		if((strcmp(p->user.name,name))==0)
			return p->user.fd;
		p=p->next;
	}
	return -1;
}

void del_like_user(int closefd,userinfo *head)
{
	userinfo *p,*q;
	p=head;
	q=p->next;
	while(q!=NULL){
		if(q->user.fd==closefd){
			printf("%s quit!\n",q->user.name);
			p->next=q->next;
			free(q);
		}
		p=q;
		q=p->next;
	}
	return ;
}

char *fd_find_name(int fd,userinfo *head)
{
	userinfo *p;
	p = head->next;
	while(p!=NULL){
		if(p->user.fd==fd)
			break;
		else
			p=p->next;
	}
	return p->user.name;
}